home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*----------------------------------------------------------------------------
- *
- * file : read.c
- *
- * Author : Yusuf Attarwala
- * Date : Sep 93
- *
- *---------------------------------------------------------------------------*/
- #include <sys/types.h>
- #include <malloc.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #include "globals_vol.h"
-
- void readAndInitData();
- void initTLut();
-
- void
- computeBoundingBox()
- {
-
- int i;
- float cenx,ceny,cenz;
-
- bminx = -(float)width/2.0;
- bmaxx = (float)width/2.0;
- bminy = -(float)height/2.0;
- bmaxy = (float)height/2.0;
- bminz = -(float)depth/2.0;
- bmaxz = (float)depth/2.0;
-
- bdiag = sqrt((bmaxx)*(bmaxx)+(bmaxy)*(bmaxy)+(bmaxz)*(bmaxz));
-
- cenx = (bmaxx+bminx)/2.0;
- ceny = (bmaxy+bminy)/2.0;
- cenz = (bmaxz+bminz)/2.0;
-
- /* compute arbitrary clipping plane variables */
- for (i=0;i<6;i++) {
- clip[i].xyz[_X] = cenx;
- clip[i].xyz[_Y] = ceny;
- clip[i].xyz[_Z] = cenz;
- }
-
- /* +x plane */
- clip[0].xyz[_X] = bminx - 0.01*(bmaxx-bminx);
- /* -x plane */
- clip[1].xyz[_X] = bmaxx + 0.01*(bmaxx-bminx);
- /* +y plane */
- clip[2].xyz[_Y] = bminy - 0.01*(bmaxy-bminy);
- /* -y plane */
- clip[3].xyz[_Y] = bmaxy + 0.01*(bmaxy-bminy);
- /* +z plane */
- clip[4].xyz[_Z] = bminz - 0.01*(bmaxz-bminz);
- /* -z plane */
- clip[5].xyz[_Z] = bmaxz + 0.01*(bmaxz-bminz);
- for (i=0;i<6;i++) {
- /* -P dot N */
- clip[i].coeff[3] = -(
- clip[i].coeff[0]*clip[i].xyz[_X] +
- clip[i].coeff[1]*clip[i].xyz[_Y] +
- clip[i].coeff[2]*clip[i].xyz[_Z] );
- }
-
- /* set the limits */
-
- clip[0].min = clip[0].xyz[_X];
- clip[0].max = clip[1].xyz[_X];
-
- clip[1].min = clip[0].xyz[_X];
- clip[1].max = clip[1].xyz[_X];
-
- clip[2].min = clip[2].xyz[_Y];
- clip[2].max = clip[3].xyz[_Y];
-
- clip[3].min = clip[2].xyz[_Y];
- clip[3].max = clip[3].xyz[_Y];
-
- clip[4].min = clip[4].xyz[_Z];
- clip[4].max = clip[5].xyz[_Z];
-
- clip[5].min = clip[4].xyz[_Z];
- clip[5].max = clip[5].xyz[_Z];
- }
-
-
- unsigned char *voxels;
- unsigned char *vptr;
- unsigned char *wptr;
- unsigned long *xy_voxels; /* xy planes */
- unsigned long *xyPtr;
-
- unsigned long *yz_voxels; /* yz planes */
- unsigned long *yzPtr;
-
- unsigned long *zx_voxels; /* zx planes */
- unsigned long *zxPtr;
-
- #define SZ(x) sizeof(x)/sizeof(float)
- static float tex[] = { TX_MAGFILTER, TX_BILINEAR,
- TX_MINFILTER, TX_BILINEAR,
- TX_WRAP, TX_REPEAT,
- TX_WRAP_S, TX_REPEAT,
- TX_WRAP_T, TX_REPEAT,
- TX_NULL,
- };
- static float env[] = { TV_MODULATE,
- TV_NULL,
- };
-
- Boolean prepare2DTexture(),
- prepare3DTexture();
-
- void
- readAndInitData(fname,w,h,d,threshold)
- char *fname;
- int w,h,d;
- int threshold;
- {
- int fd;
-
- width = w;
- height = h;
- depth = d;
-
- vptr = voxels = (unsigned char *) malloc((width*height*depth));
-
- if ( (fd = open(fname, O_RDONLY)) < 0 ) {
- perror("volTex: can't open file");
- exit(-1);
- }
-
- read(fd, voxels, width*height*depth );
- close( fd );
- fprintf(stderr,"volTex : reading finished\n");
-
-
- /* define the polygon dimensions on which the texture will be mapped */
- /* xy plane */
- xy_v0[0] = -width/2.0;
- xy_v0[1] = -height/2.0;
- xy_v0[2] = 0.0;
-
- xy_v1[0] = width/2.0;
- xy_v1[1] = -height/2.0;
- xy_v1[2] = 0.0;
-
- xy_v2[0] = width/2.0;
- xy_v2[1] = height/2.0;
- xy_v2[2] = 0.0;
-
- xy_v3[0] = -width/2.0;
- xy_v3[1] = height/2.0;
- xy_v3[2] = 0.0;
-
- /* yz plane */
- yz_v0[0] = 0.0;
- yz_v0[1] = -height/2.0;
- yz_v0[2] = -depth/2.0;
-
- yz_v1[0] = 0.0;
- yz_v1[1] = height/2.0;
- yz_v1[2] = -depth/2.0;
-
- yz_v2[0] = 0.0;
- yz_v2[1] = height/2.0;
- yz_v2[2] = depth/2.0;
-
- yz_v3[0] = 0.0;
- yz_v3[1] = -height/2.0;
- yz_v3[2] = depth/2.0;
-
- /* zx plane */
- zx_v0[0] = -width/2.0;
- zx_v0[1] = 0.0;
- zx_v0[2] = -depth/2.0;
-
- zx_v1[0] = -width/2.0;
- zx_v1[1] = 0.0;
- zx_v1[2] = depth/2.0;
-
- zx_v2[0] = width/2.0;
- zx_v2[1] = 0.0;
- zx_v2[2] = depth/2.0;
-
- zx_v3[0] = width/2.0;
- zx_v3[1] = 0.0;
- zx_v3[2] = -depth/2.0;
- }
-
- Boolean
- prepare2DTexture()
- {
-
- int i,j,k;
- unsigned char *qptr, *qp;
-
- /* flip the images */
- qp = qptr = (unsigned char *) malloc( (width*height*depth));
- if (!qp) {
- fprintf(stderr,"vol2d : could not allocate memory \n");
- exit(2);
- }
- memcpy(qptr,voxels,width*height*depth);
-
- j = width*height*depth;
- vptr = voxels+width*height*depth;
- for (i=0;i<j;i++) {
-
- #ifdef LATER
- /* apply threshold here */
- if (threshold) {
- if (threshold < 0) {
- if (*qptr > -threshold) *qptr = 0;
- *qptr *= 2.0;
- }
- else {
- if (*qptr < threshold) *qptr = 0;
- }
-
- if (*qptr > 256) *qptr = 256;
- if (*qptr < 0) *qptr = 0;
- }
- #endif
-
- *vptr-- = *qptr++;
- }
- if (qp) free(qp);
-
- /* stuff this data into a two component texture */
-
- /* xy plane */
-
- printf("setting up xy textures...\n");
-
- xyPtr = xy_voxels = (unsigned long *) malloc((width*height*depth/2)
- *sizeof(unsigned long));
-
- for (i=0;i<depth;i++) {
- for (j=0;j<height;j++) {
- for (k=0;k<width; k+=2) {
- vptr = voxels + i*width*height + j*width + k;
- *xyPtr = 0;
- *xyPtr++ = ((*vptr)<<24) | ((*(vptr))<<16) |
- ((*(vptr+1))<<8) | ((*(vptr+1)));
- }
- }
- }
-
- /* the unopti uses too many texbinds, each slice is one texture,
- whereas opti stuffs all the slices of each set on one texture */
-
- #ifdef OPTI
- for (i=0;i<depth;i++) {
- texdef2d(i+1,2,width,height,(xy_voxels + i*(width*height/2)),SZ(tex),tex);
- }
- #else
- texdef2d(100,2,width,height*depth,xy_voxels,SZ(tex),tex);
- #endif
- free(xy_voxels);
-
- /* yz plane */
-
- printf("setting up yz textures...\n");
-
- yzPtr = yz_voxels = (unsigned long *) malloc((width*height*depth/2)
- *sizeof(unsigned long));
- for (i=0;i<width;i++) {
- for (j=0;j<depth;j++) {
- for (k=0;k<height; k+=2) {
- vptr = voxels + (width*height)*(depth-1-j) + k*width + (width-i);
- wptr = voxels + (width*height)*(depth-1-j) + (k+1)*width + (width-i);
- *yzPtr = 0;
- *yzPtr++ = ((*vptr)<<24) | ((*(vptr))<<16) |
- ((*wptr)<<8) | (*wptr);
- }
- }
- }
-
- #ifdef OPTI
- for (i=0;i<width;i++) {
- texdef2d(400+i+1,2,height,depth,(yz_voxels + i*(height*depth/2)),SZ(tex),
- tex);
- }
- #else
- texdef2d(400,2,height,depth*width,yz_voxels,SZ(tex),tex);
- #endif
- free(yz_voxels);
-
- /* zx plane */
-
- printf("setting up zx textures...\n");
-
- zxPtr = zx_voxels = (unsigned long *) malloc((width*height*depth/2)
- *sizeof(unsigned long));
- for (i=0;i<height;i++) {
- for (j=0;j<width;j++) {
- for (k=0;k<depth; k+=2) {
- vptr = voxels + (width*height)*(depth-1-k) + width*(height-1-i) + j;
- wptr = voxels + (width*height)*(depth-1-k-1) + width*(height-1-i) + j;
- *zxPtr = 0;
- *zxPtr++ = ((*vptr)<<24) | ((*(vptr))<<16) |
- ((*wptr)<<8) | (*wptr);
- }
- }
- }
-
- #ifdef OPTI
- for (i=0;i<height;i++) {
- texdef2d(800+i+1,2,depth,width,(zx_voxels + i*(depth*width/2)),SZ(tex),
- tex);
- }
- #else
- texdef2d(800,2,depth,width*height,zx_voxels,SZ(tex),tex);
- #endif
- free(zx_voxels);
-
- free(voxels);
-
- tevdef (1,SZ(env),env);
- tevbind(TV_ENV0,1);
-
- return(1);
- }
-
- /* 8 bit texture */
- static float texProps8[] = {TX_MAGFILTER, TX_TRILINEAR,
- TX_MINFILTER, TX_TRILINEAR,
- TX_WRAP, TX_CLAMP,
- TX_INTERNAL_FORMAT, TX_IA_8,
- TX_NULL,
- };
-
- /* 4 bit texture */
- static float texProps4[] = {TX_MAGFILTER, TX_TRILINEAR,
- TX_MINFILTER, TX_TRILINEAR,
- TX_WRAP, TX_CLAMP,
- /*
- TX_INTERNAL_FORMAT, TX_RGBA_4,
- */
- TX_NULL,
- };
-
-
- static float envProps[] = { TV_MODULATE,
- /*TV_COLOR,0.75,0.13,0.06,1.,TV_BLEND,*/
- TV_NULL,
- };
-
- /* properties for texture look up table */
- static float tlutProps[] = { TL_NULL };
- static u_long tlut[256];
-
- /* XXX3 */
-
- Boolean
- prepare3DTexture()
- {
- /* define a 3d texture, one component */
- int i,j,k;
- unsigned char *qptr, *qp;
-
- /* flip the images */
- qp = qptr = (unsigned char *) malloc( (width*height*depth));
- if (!qp) {
- fprintf(stderr,"vol2d : could not allocate memory \n");
- exit(2);
- }
- memcpy(qptr,voxels,width*height*depth);
-
- j = width*height*depth;
- vptr = voxels+width*height*depth;
- for (i=0;i<j;i++) {
-
- #ifdef LATER
- /* apply threshold here */
- if (threshold) {
- if (threshold < 0) {
- if (*qptr > -threshold) *qptr = 0;
- *qptr *= 2.0;
- }
- else {
- if (*qptr < threshold) *qptr = 0;
- }
-
- if (*qptr > 256) *qptr = 256;
- if (*qptr < 0) *qptr = 0;
- }
- #endif
-
- *vptr-- = *qptr++;
- }
- if (qp) free(qp);
-
- printf("setting up 3d textures...\n");
-
- texdef3d(
- 1, /* texture Id */
- 1, /* number of components */
- width, /* width */
- height, /* height */
- depth, /* depth */
- (unsigned long *) voxels,
- #ifdef NO
- SZ(texProps4), /* np */
- #endif
- 0,
- texProps4 );
-
- initTLut(1);
- tlutbind(0, 1);
-
- #ifdef TEXGEN
- {
- static float S_plane[] = { 1., 0., 0., .5 };
- static float T_plane[] = { 0., 1., 0., .5 };
- static float R_plane[] = { 0., 0., 1., .5 };
- static float Q_plane[] = { 0., 0., 0., 1. };
-
-
- texgen(TX_S, TG_LINEAR, S_plane);
- texgen(TX_T, TG_LINEAR, T_plane);
- texgen(TX_R, TG_LINEAR, R_plane);
- texgen(TX_Q, TG_LINEAR, Q_plane);
-
- texgen(TX_S, TG_ON, S_plane);
- texgen(TX_T, TG_ON, T_plane);
- texgen(TX_R, TG_ON, R_plane);
- texgen(TX_Q, TG_ON, Q_plane);
- }
- #endif
-
- tevdef (1,SZ(envProps),envProps);
- tevbind(TV_ENV0,1);
-
- texbind(TX_TEXTURE_0,1);
-
- return(1);
- }
-
- void
- initTLut(indx)
- int indx;
- {
- int i,j;
- long t[256][4];
- for (i=0;i<256;i++) {
- for(j=0;j<4;j++){
- t[i][j] = i;
- }
- }
-
- for(i=0;i<256;i++){
- tlut[i] = (long) (
- (long) ((t[i][3] << 24) & 0xff000000) |
- (long) ((t[i][2] << 16) & 0x00ff0000) |
- (long) ((t[i][1] << 8) & 0x0000ff00) |
- (long) ((t[i][0] ) & 0x000000ff));
- }
-
- tlutdef(indx, 4, 256, tlut, 0, tlutProps);
- }
-
-
-
- void
- outofDegenerate()
- {
- drawMode = TEXTURED;
- refresh = 1;
- drawScene();
- }
-
- void
- intoDegenerate()
- {
- drawMode = WIREFRAME;
- refresh = 1;
- drawScene();
- }
-